/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;
/**
 *
 * @author Chong & Ali
 */
import java.util.ArrayList;
import java.awt.Graphics;

public class Level {
    public static final int PIXEL = 32;    
    
    private Location[][] locations = new Location[13][22];
    
    private int width = 0;
    private int height = 0; 
    
    private Player player;
    private Map map;
    
    public static ArrayList<Goal> goals = new ArrayList<Goal>();
    public static int goalComplete = 0;
    public static boolean completed;
    
        
    public Level(int mapNr){        
        initLevel(mapNr);
        setNeighbours();
        getlocations();
        completed = false;
    }

    public final void initLevel(int mapNr) {
        int x = 0;
        int y = 0;
        
        map = new Map();
        
        for(int i = 0; i < map.getMap(mapNr).length(); i++) {
            char character = map.getMap(mapNr).charAt(i);
            
            if(character == '\n') {
                y += 1;
                
                if (this.width < x) {
                    this.width = x;
                }
                x = 0;

            }else if(character == '#') {  
                Location loc = new Location(x*PIXEL, y*PIXEL);
                locations[y][x] = loc;
                Wall wall = new Wall(loc);
                loc.setGameObject(wall);
                x = x + 1;                
            }else if(character == '$') { 
                Location loc = new Location(x*PIXEL, y*PIXEL);
                locations[y][x] = loc;
                Box box = new Box(loc);
                FreeSpace freespace = new FreeSpace(loc);
                loc.setGameObject(box);
                loc.setFloor(freespace);
                x = x + 1;          
            }else if(character == '.') {
                Location loc = new Location(x*PIXEL, y*PIXEL);
                locations[y][x] = loc;
                Goal goal = new Goal(loc);
                loc.setFloor(goal);
                goals.add(goal);
                x = x + 1;               
            }else if(character == '@') { 
                Location loc = new Location(x*PIXEL, y*PIXEL);
                locations[y][x] = loc;
                player = new Player(loc);
                FreeSpace floor = new FreeSpace(loc);
                loc.setGameObject(player);
                loc.setFloor(floor);
                x = x + 1;     
            }else if(character == ' ') { 
                Location loc = new Location(x*PIXEL, y*PIXEL);
                locations[y][x] = loc;
                FreeSpace floor = new FreeSpace(loc);
                loc.setFloor(floor);
                x = x + 1;             
            }else if(character == '-') {
                Location loc = new Location(x*PIXEL, y*PIXEL);
                locations[y][x] = loc;
                x += 1;
            }
            height = y;
        }
    }
       
    public final void setNeighbours(){        
        for(int y = 0; y < locations.length; y++){
            for(int x = 0; x <locations[y].length; x++){
                if (y != 0){
                    //initialize upperNeighboards
                    locations[y][x].setNeighbour("north", locations[y-1][x]);
                }
            }
        }       
        for(int y = 0; y < locations.length; y++){
           for(int x = 0; x < locations[y].length; x++){
               if (x != width-1){
                    //initialize rightNeighboards
                    locations[y][x].setNeighbour("east", locations[y][x+1]);
               }
           }
       }
       for(int y = 0; y < locations.length; y++){
           for(int x = 0; x < locations[y].length; x++){
               if (y != height-1){
                   //initialize belowNeighboards
                   locations[y][x].setNeighbour("south", locations[y+1][x]); 
               }
           }
       }
        for(int y = 0; y < locations.length; y++){
           for(int x = 0; x < locations[y].length; x++){
               if (x != 0){
                    //initialize leftNeighboards
                   locations[y][x].setNeighbour("west", locations[y][x-1]);
                }
            }
        }
    }
     
    public void paintLevel(Graphics g){
        for(int y = 0; y < locations.length; y++){
            for(int x = 0; x < locations[y].length ;x++){ 
                // painting all floors
                if(locations[y][x].getFloor() != null){
                    Floor floor = locations[y][x].getFloor();
                    g.drawImage(floor.getImage(), floor.getLocation().getXpos(), floor.getLocation().getYpos(), PIXEL, PIXEL, null);
                } 
                // painting all gameObjects
                if (locations[y][x].getGameObject() != null){
                    GameObject object = locations[y][x].getGameObject();
                    g.drawImage(object.getImage(), object.getLocation().getXpos(), object.getLocation().getYpos(), PIXEL, PIXEL, null);
                } 
            }
        }
    }
    
    public final Location[][] getlocations(){
        return locations;
    }
    
    public Location getlocation(int x, int y){
        return locations[y][x];
    }
       
    public Player getPlayer(){
        return player;
    }
        

    public static boolean isCompleted() {
        if (goalComplete == goals.size()){
           System.out.println("LEVEL COMPLETED!");
           completed = true;
        }
        return completed;
    }
}

